package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.domain.FactoryTeamGroup;
import com.uwlaser.service.FactoryTeamGroupService;
import com.uwlaser.service.dto.FactoryTeamGroupDto;
import com.uwlaser.service.dto.FactoryTeamGroupQueryParam;
import com.uwlaser.service.mapper.FactoryTeamGroupMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author huangpan
 * @date 2022-12-02
 */
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FactoryTeamGroupService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FactoryTeamGroupServiceImpl extends CommonServiceImpl<FactoryTeamGroupMapper, FactoryTeamGroup> implements FactoryTeamGroupService {

    // private final RedisUtils redisUtils;
    private final FactoryTeamGroupMapper factoryTeamGroupMapper;

    @Override
    public PageInfo<FactoryTeamGroupDto> queryAll(FactoryTeamGroupQueryParam query, Pageable pageable) {
        IPage<FactoryTeamGroup> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FactoryTeamGroup> page = factoryTeamGroupMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, FactoryTeamGroupDto.class);
    }

    @Override
    public List<FactoryTeamGroupDto> queryAll(FactoryTeamGroupQueryParam query) {
        return ConvertUtil.convertList(factoryTeamGroupMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FactoryTeamGroupDto.class);
    }

    @Override
    public FactoryTeamGroup getById(Integer id) {
        return factoryTeamGroupMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FactoryTeamGroupDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), FactoryTeamGroupDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FactoryTeamGroupDto resources) {
        FactoryTeamGroup entity = ConvertUtil.convert(resources, FactoryTeamGroup.class);

        //查询该编码是否存在
        if (factoryTeamGroupMapper.selectOne(new LambdaQueryWrapper<FactoryTeamGroup>()
                .eq(FactoryTeamGroup::getTeamGroupCode, resources.getTeamGroupCode())) != null) {
            throw new RuntimeException("当前班组" + resources.getTeamGroupCode() + "编码已存在");
        }

        return factoryTeamGroupMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(FactoryTeamGroupDto resources) {
        FactoryTeamGroup entity = ConvertUtil.convert(resources, FactoryTeamGroup.class);
        //获取当前数据
        FactoryTeamGroup factoryTeamGroup = factoryTeamGroupMapper.selectById(resources.getId());
        if (!resources.getTeamGroupCode().equals(factoryTeamGroup.getTeamGroupCode())) {
            //查询该编码是否存在
            if (factoryTeamGroupMapper.selectOne(new LambdaQueryWrapper<FactoryTeamGroup>()
                    .eq(FactoryTeamGroup::getTeamGroupCode, resources.getTeamGroupCode())) != null) {
                throw new RuntimeException("当前班组" + resources.getTeamGroupCode() + "编码已存在");
            }
        }
        int ret = factoryTeamGroupMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids) {
        // delCaches(ids);
        return factoryTeamGroupMapper.deleteBatchIds(ids);
    }

    @Override
    public List<FactoryTeamGroupDto> getTeamOrGroup() {
        return factoryTeamGroupMapper.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id) {
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<FactoryTeamGroupDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (FactoryTeamGroupDto factoryTeamGroup : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("班组编码", factoryTeamGroup.getTeamGroupCode());
              map.put("班组名称", factoryTeamGroup.getTeamGroupName());
              map.put("是否禁用 0：否，1：是", factoryTeamGroup.getIsDisable());
              map.put("备注说明", factoryTeamGroup.getRemark());
              map.put("创建人", factoryTeamGroup.getCreateBy());
              map.put("创建时间", factoryTeamGroup.getCreateTime());
              map.put("更新人", factoryTeamGroup.getUpdateBy());
              map.put("更新时间", factoryTeamGroup.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
